﻿using AutoMapper;
using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys
{

    /// <summary>
    /// 添加图片命令处理
    /// </summary>
    public class AddImgsCommandhandlter : IRequestHandler<AddImgsCommand, ApiResult<int>>
    {
        /// <summary>
        /// 图片仓储
        /// </summary>
        private readonly IBaseRepository<ImagesModel> imgRep;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="ImgRep">图片仓储</param>
        /// <param name="mapper">映射</param>
        public AddImgsCommandhandlter(IBaseRepository<ImagesModel> ImgRep,IMapper mapper)
        {
            imgRep = ImgRep;
            this.mapper = mapper;
        }


        public async Task<ApiResult<int>> Handle(AddImgsCommand request, CancellationToken cancellationToken)
        {


            var imgs = mapper.Map<ImagesModel>(request);
            await imgRep.AddAsync(imgs);

            return new ApiResult<int>
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "添加成功"
            };
        }
    }
}
